package cn.cvs.controller;

import cn.cvs.pojo.Blog;
import cn.cvs.pojo.Pet;
import cn.cvs.pojo.Review;
import cn.cvs.service.adoptanimal.AdoptanimalService;
import cn.cvs.service.blog.BlogService;
import cn.cvs.service.pet.PetService;
import cn.cvs.service.review.ReviewService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/review")
public class ReviewController {

    @Resource
    private ReviewService reviewService;
    @Resource
    private AdoptanimalService adoptanimalService;

    @RequestMapping(value="/list")
    public String queryPageList(Model model,
                                @RequestParam(value = "state",required = false) Integer state,
                                @RequestParam(value ="pageIndex", defaultValue = "1") Integer pageIndex ) {
        //设置页面容量
        Integer pageSize = Constants.pageSize;

        int totalCount = reviewService.count(state);

        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(pageIndex);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();

        List<Review> blogList = reviewService.list(state, pageIndex, pageSize);

        if(pageIndex > totalPageCount){
            pageIndex = totalPageCount;
        }

        model.addAttribute("reviewList", blogList);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", pageIndex);

        return "review/list";
    }


    @DeleteMapping(value="/del/{id}")
    @ResponseBody
    public Map<String,String> delete(@PathVariable(value = "id",required = true)Integer id){
        Map<String,String> resultMap = new HashMap<>();
        int result = reviewService.deleteById(id);
        if (result > 0) {
            resultMap.put("result","true");
        } else {
            resultMap.put("result","false");
        }
        return resultMap;
    }


    /**
     * 跳转到添加页面
     * @param
     * @return
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        List<Pet> pets = adoptanimalService.selectPetList();
        model.addAttribute("pets",pets);
        return "review/add";
    }

    /**
     * 新增回访活动信息
     * @param review
     * @return
     */
    @PostMapping("/add")
    public String add(Review review){
        int result = reviewService.insert(review);
        if(result <= 0){
            return "review/add";
        }
        return "redirect:/sys/review/list";
    }

    /**
     * 修改回访活动信息
     * @param review
     * @return
     */
    @PostMapping("/modify")
    public String modify(Review review){
        int result = reviewService.updateById(review);
        if(result <= 0){
            return "review/update";
        }
        return "redirect:/sys/review/list";
    }

    /**
     * 查询回访详情信息
     * @param id
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable Integer id, Model model, HttpServletRequest request){
        Review review = reviewService.selectById(id);
        List<Pet> pets = adoptanimalService.selectPetList();
        model.addAttribute("review",review);
        model.addAttribute("pets",pets);
        return "review/update";
    }
}
